<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/8/15 15:02
 * Modified Time: 2019/8/15 15:02
 * Version: 2.0
 */

namespace App\Modules\Store\Criterias;


use Flower\Core\Repository\Contracts\RepositoryInterface;
use Flower\Core\Repository\Criterias\BaseCriterias;

class GetStoreResourceCriteria extends BaseCriterias
{
    protected $store_id;

    public function __construct($store_id)
    {
        $this->store_id = $store_id;
    }

    public function apply($model, RepositoryInterface $repository)
    {
        $store_id = $this->store_id;
        $select = [
            'resource',
            'type',
            'CASE WHEN type=1 THEN hx_picture.image WHEN type=2 THEN hx_video.thumb END as image',
            'CASE WHEN type=2 THEN hx_video.url END as url'
        ];
        $select = implode($select,',');
        return $model->selectRaw($select)
            ->leftJoin('picture','store_resource.resource','picture.id')
            ->leftJoin('video','store_resource.resource','video.id')
            ->where('store_id',$store_id);

    }

}